package com.mooral.web.action.application;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mooral.bean.Bean;
import com.mooral.bean.Categories;
import com.mooral.bean.Vouchers;
import com.mooral.context.Context;
import com.mooral.web.action.MooralAction;
import com.mooral.web.util.SorterUtils;

public class WelcomeAction extends MooralAction {

	@Override
	public ActionForward perform(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			Context context) throws Exception {

		// Categorias que se visualizan el content.jsp
		// TODO sacar esta chanchada cuando definamos politicas de scope application
		if(request.getSession().getServletContext().getAttribute("categoriesRoot") == null){
			Categories categories = new Categories();
			categories.populate("select * FROM categories where parent_id is null order by name");
			request.getSession().getServletContext().setAttribute("categoriesRoot",  categories);
		}
		
		// 10 + populares
		// TODO sacar esta chanchada cuando definamos politicas de scope application
		if(request.getSession().getServletContext().getAttribute("vouchersMostPopular10") == null){
			Vouchers vouchers = new Vouchers();
			vouchers.populate("SELECT 	id, " +
					"					merchant_id, " +
					"					category_id, " +
					"					type, " +
					"					source, " +
					"					url, " +
					"					expire, " +
					"					access, " +
					"					access_day, " +
					"					created, " +
					"					modified, " +
					"					image, " +
					"					if(length(title) > 45, concat(substr(title, 1, 50), '...'), title) as title, " +
					"					slug " +
					"			FROM 	vouchers " +
					"			WHERE 	expire >= NOW() " +
					"			ORDER BY access DESC limit 10");
			request.getSession().getServletContext().setAttribute("vouchersMostPopular10", vouchers);
		}
		
		
		// 10 ofertas exclusivas
		// TODO sacar esta chanchada cuando definamos politicas de scope application
		if(request.getSession().getServletContext().getAttribute("vouchersExclusiveDeals10") == null){
			Vouchers vouchers = new Vouchers();
			vouchers.populate("SELECT 	id, " +
					"					merchant_id, " +
					"					category_id, " +
					"					type, " +
					"					source, " +
					"					url, " +
					"					expire, " +
					"					access, " +
					"					access_day, " +
					"					created, " +
					"					modified, " +
					"					image, " +
					"					if(length(title) > 45, concat(substr(title, 1, 50), '...'), title) as title, " +
					"					slug " +
					"			FROM 	vouchers " +
					"			WHERE type='Deal' AND expire >= NOW() " +
					"			ORDER BY created DESC limit 10");
			request.getSession().getServletContext().setAttribute("vouchersExclusiveDeals10", vouchers);
		}
		
		return mapping.findForward(ACTION_SUCCESS);
	}
}
